home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / C++ Toolbox 1.0 / Memory++.h < prev    next >
C/C++ Source or Header  |  1996-03-09  |  3KB  |  175 lines

  1. #pragma once
  2. /*
  3.     File:        Memory++.h
  4.     
  5.     Contents:    C++ extensions to the Macintosh interfaces.
  6.     
  7.     Version:    1.0.1 (for System 7.x)
  8.     
  9.     Copyright:    ©1995 Chris K. Thomas.  All Rights Reserved.
  10.     
  11.     
  12.     1.0.1  ckt    December 15, 1995     add direct handle accessor methods (sethandle/gethandle)
  13. */
  14.  
  15. #include "Types++.h"
  16.  
  17. //
  18. //    A wrapper class for arrays contained within
  19. //    Mac handles;  purposely a very light runtime
  20. //    implementation.
  21. //
  22. template <class T>
  23. class HandleArray
  24. {
  25.     Boolean    weOwnHandle;
  26.     T        **ourHandle;
  27.     
  28. public:
  29.     HandleArray(T** inHandle)
  30.     {
  31.         ourHandle = inHandle;
  32.         weOwnHandle = false;
  33.     }
  34.     
  35.     HandleArray()
  36.     {
  37.         ourHandle = reinterpret_cast<T **>(NewHandle(0));
  38.         if(ourHandle == NULL)
  39.             Throw_(MemError());
  40.         
  41.         weOwnHandle = true;
  42.     }
  43.     
  44.     HandleArray(long inCount)
  45.     {
  46.         ourHandle = reinterpret_cast<T **>(NewHandleClear(sizeof(T) * inCount));
  47.         if(ourHandle == NULL)
  48.             Throw_(MemError());
  49.         
  50.         weOwnHandle = true;
  51.     }
  52.     
  53.     ~HandleArray()
  54.     {
  55.         if(ourHandle && *ourHandle && weOwnHandle)
  56.         {
  57.             DisposeHandle(*this);
  58.         }
  59.     }
  60.                                                     // • subscript operator
  61.                                                     // allows access as a first-class array
  62.     T & operator[](long inIndex)
  63.     {
  64.         if(!(inIndex >= 0 && inIndex < getcount()))    // bounds checking
  65.         {
  66.             DebugNum(inIndex);
  67.             DebugNum(getcount());
  68.             Throw_('asrt');
  69.         }
  70.         return (*ourHandle)[inIndex];
  71.     }
  72.                                                     // * conversion operators
  73.     operator Handle()
  74.     {
  75.         return reinterpret_cast<Handle>(ourHandle);
  76.     }
  77.     
  78.     operator T**()
  79.     {
  80.         return reinterpret_cast<T**>(ourHandle);
  81.     }
  82.     
  83.                                                     // * accessors
  84.     void lock()
  85.     {
  86.         HLockHi(reinterpret_cast<Handle>(ourHandle));
  87.     }
  88.     
  89.     void unlock()
  90.     {
  91.         HUnlock(reinterpret_cast<Handle>(ourHandle));
  92.     }
  93.     
  94.     void sethandle(T **inNewHandle)
  95.     {
  96.         ourHandle = inNewHandle;
  97.     }
  98.     
  99.     T **gethandle()
  100.     {
  101.         return ourHandle;
  102.     }
  103.     
  104.     void setcount(SInt32 inCount)
  105.     {
  106.         Assert_(inCount >= 0);
  107.         char handleState;
  108.         
  109. //        DebugNum(sizeof(T));
  110.         handleState = HGetState(reinterpret_cast<Handle>(ourHandle));
  111.         unlock();
  112.         SetHandleSize(reinterpret_cast<Handle>(ourHandle), inCount * sizeof(T));
  113.         
  114.         long ourErr = MemError();
  115.         if(ourErr != noErr)
  116.         {
  117.             HSetState(reinterpret_cast<Handle>(ourHandle), handleState);
  118.             Throw_(ourErr);
  119.         }
  120.         
  121.         HSetState(reinterpret_cast<Handle>(ourHandle), handleState);
  122.     }
  123.     
  124.     SInt32 getcount()
  125.     {
  126.         return (GetHandleSize(reinterpret_cast<Handle>(ourHandle)) / sizeof(T));
  127.     }
  128. };
  129.  
  130.  
  131. //
  132. // wrapper class for a standard pointer
  133. //
  134.  
  135. template <class T>
  136. class Pointer
  137. {
  138. public:
  139.     Boolean    own;
  140.     T        *ptr;
  141.     
  142.     Pointer(Size inSize)
  143.     {
  144.         ptr = (T *)NewPtrClear(inSize);
  145.         if(ptr == NULL)
  146.             Throw_(MemError());
  147.         
  148.         own = true;
  149.     }
  150.     
  151.     Pointer(T *inPtr)
  152.     {
  153.         ptr = inPtr;
  154.         own = false;
  155.     }
  156.     
  157.     ~Pointer()
  158.     {
  159.         if(own)
  160.         {
  161.             DisposePtr((Ptr) ptr);
  162.         }
  163.     }
  164.     
  165.     operator T *()
  166.     {
  167.         return ptr;
  168.     }
  169.     
  170.     operator Ptr()
  171.     {
  172.         return (Ptr)ptr;
  173.     }
  174. };
  175.